package com.lc.dao.impl;

import com.lc.bean.Commodity;
import com.lc.dao.CommodityDao;
import com.lc.util.BaseUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: ManolinCoder
 * @time: 2021/8/19
 */
public class CommodityDaoImpl extends BaseUtil implements CommodityDao {

    @Override
    public List<Commodity> selectAllCommodities() {
        ArrayList<Commodity> commodities = new ArrayList<>();
        String sql = "select * from commodity";
        ResultSet resultSet = query(sql,null);
        try {
            while (resultSet.next()){
                Commodity commodity = new Commodity();
                commodity.setId(resultSet.getInt("id"));
                commodity.setCommodityName(resultSet.getString("commodityName"));
                commodity.setCommodityPrice(resultSet.getFloat("commodityPrice"));
                commodity.setCommoditySale(resultSet.getInt("commoditySale"));
                commodities.add(commodity);
            }
        }catch (SQLException e){
            e.printStackTrace();
            return null;
        }finally {
            closeall();
        }
        if (commodities.size()==0){
            return null;
        }else {
            return commodities;
        }
    }
}
